package com.px.project.module.ht.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.px.common.dto.BaseQueryDto;
import com.px.common.enums.PlanOperationStatusEnum;
import com.px.common.util.AssertUtil;
import com.px.common.util.CommonUtil;
import com.px.common.config.security.LoginContextHolder;
import com.px.project.module.ht.dto.DayPlanQueryDto;
import com.px.project.module.ht.vo.DayPlanVo;
import com.px.project.module.ht.entity.DayPlan;
import com.px.project.module.ht.service.DayPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 后台管理》施工计划》计划销点
 *
 * @author 品讯科技
 * @since 2023-10-06
 */
@RestController
@RequestMapping("/planWriteOff")
public class PlanWriteOffController {

    @Autowired
    private DayPlanService dayPlanService;


    /**
     * 分页查询
     *
     * @param pageVo 页面 VO
     * @param param  参数
     * @return {@link Page }<{@link DayPlanVo }>
     * @author 品讯科技
     * @date 2024-08
     */
    @GetMapping("/page")
    public Page<DayPlanVo> page(@Validated BaseQueryDto<DayPlanVo> pageVo, DayPlanQueryDto param) {
        CommonUtil.setNullValue(param);
        return dayPlanService.selectPageListForWriteOff(pageVo, param);
    }

    /**
     * 销点通过
     *
     * @param id id
     * @return {@link Boolean }
     * @author 品讯科技
     * @date 2024-08
     */
    @PostMapping("/pass/{id}")
    public Boolean clickPass(@PathVariable("id") Long id) {
        DayPlan plan = dayPlanService.getById(id);
        AssertUtil.checkArgument(ObjectUtil.equal(plan.getOperationStatus(),PlanOperationStatusEnum.TO_WRITE_OFF.getCode()), "该计划未处于“施工中【代销点】”状态，无法销点通过");
        plan.setOperationStatus(PlanOperationStatusEnum.WRITE_OFF.getCode());
        plan.setWriteOffApproveTime(new Date());
        plan.setWriteOffApproveUserId(LoginContextHolder.me().getId());
        return dayPlanService.updateById(plan);
    }

}
